home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-08-18 | 3.7 KB | 167 lines | [TEXT/CWIE] |
- // ZString.cpp
- // 11/12/96
-
- #include "ZString.h"
-
- #define MIN(a,b) ((a)<(b)? a:b)
-
- // string comparison function
- // return 1 if s1>s2, -1 if s1<s2, 0 if equal
- short JStrcmp( const ZString& s1, const ZString& s2 )
- {
- for (int i=0; i<s1.buf[0] && i<s2.buf[0]; i++) {
- if (s1.buf[i] != s2.buf[i]) {
- return (s1.buf[i] < s2.buf[i] ? -1 : 1);
- }
- }
- if (s1.buf[0]==s2.buf[0]) return 0;
- return (s1.buf[0] < s2.buf[0] ? -1 : 1);
- }
-
- // constructors
-
- ZString::ZString( const long pNum )
- {
- NumToString( pNum, buf );
- }
-
- long IntValue( const ZString &s )
- {
- long temp=0;
- StringToNum( s, &temp );
- return temp;
- }
-
- ZString Plural( const ZString &s )
- {
- if (s.Right()=="s" || s.Right(2)=="ch" || s.Right(2)=="sh" || s.Right()=="x") return s+"es";
- if (s.Right()=="y" && s.Right(2)!="ey") return s.Left(s.Length()-1)+"ies";
- return s+"s";
- }
-
- ZString& ZString::operator= ( const ZString& pStr )
- {
- BlockMove(pStr.buf, buf, pStr.buf[0] + 1L);
- return *this;
- }
-
- void ZString::StuffPstr( unsigned char* pPstr, const int maxlen )
- {
- long charsToMove = MIN(maxlen-1, buf[0]);
- BlockMove(buf + 1, pPstr + 1, charsToMove);
- pPstr[0] = charsToMove;
- }
-
- ZString& ZString::operator= ( const char* pChars )
- {
- short i=0;
- for (; pChars[i]; i++) {
- buf[i+1] = pChars[i];
- }
- buf[0] = i;
- return *this;
- }
-
- void ZString::StuffChars( char* pChars, const int maxlen )
- {
- long charsToMove = MIN(maxlen-1, buf[0]);
- BlockMove(buf + 1, pChars, charsToMove);
- pChars[charsToMove] = 0;
- }
-
- ZString& ZString::operator= ( const Str255 pPstr )
- {
- BlockMove(pPstr, buf, pPstr[0] + 1L);
- return *this;
- }
-
- ZString& ZString::operator+= ( const ZString& pStr )
- {
- long charsToCopy;
-
- // Truncate if concatenated string would be longer than 255 chars.
- charsToCopy = MIN(pStr.buf[0], 255 - buf[0]);
- BlockMove(pStr.buf + 1, buf + buf[0] + 1, charsToCopy);
- buf[0] += charsToCopy;
- return *this;
- }
-
- ZString ZString::Substr ( const int pFrom, const int pTo ) const
- {
- int from = (pFrom < 1 ? 1 : pFrom);
- if (from > buf[0]) return "\p";
- int to = (pTo > buf[0]+1 ? buf[0]+1 : pTo);
- if (from > to) return "\p";
-
- ZString temp;
- temp.buf[0] = to-from;
- BlockMove(buf+from, temp.buf+1, temp.buf[0]);
- return temp;
- }
-
- ZString ZString::Element( const int pElem, const char pDelim ) const
- {
- short i=0, p=0;
-
- // find occurances of pDelim in the string until i=pElem
- while (p<buf[0] && i<pElem) {
- if (buf[++p] == pDelim) i++;
- }
-
- // if i<pElem, then there aren't that many elements in the string
- if (i<pElem) return "\p";
-
- // otherwise, return a string from here to the next occurance of the element
- short p2=p+1;
- while (p2<=buf[0] && buf[p2]!=pDelim) p2++;
- return (*this)(p+(p!=0), p2);
- }
-
- ZString ZString::Trim( void ) const
- {
- // trim leading and trailing whitespace from the string
- short p1=1;
- while (p1<buf[0] && buf[p1]!=' ' && buf[p1]!='\t' && buf[p1]!='n') p1++;
-
- short p2=buf[0];
- while (p2>0 && buf[p2]!=' ' && buf[p2]!='\t' && buf[p2]!='n') p2++;
-
- return (*this)(p1, p2-1);
- }
-
- ZString ZString::Compress( void ) const
- {
- // change all tabs and multiple spaces into single spaces
- ZString temp;
- Boolean onSpace = 0;
- const unsigned char *src = buf+1;
- unsigned char *dest = temp.buf+1;
- while (*src) {
- if (*src == ' ' || *src == '\t') {
- if (!onSpace) {
- // start of a new space run...
- onSpace = 1;
- *dest++ = ' ';
- src++;
- } else {
- // continuation of a space run -- don't copy; just advance the src pointer
- src++;
- }
- } else {
- // non-whitespace; copy char and terminate space run flag
- *dest++ = *src++;
- onSpace = 0;
- }
- }
- temp[0] = dest-temp;
- return temp;
- }
-
- ZString Uppercase( const ZString& pStr )
- {
- ZString out(pStr);
- for (short i=1; i<pStr.buf[0]; i++)
- if (pStr.buf[i] >= 'a' && pStr.buf[i] <= 'z') out.buf[i] -= ('a'-'A');
- return out;
- }
-